package com.example.sharingjdbc4digiwin.config;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import com.google.common.collect.Maps;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.Map;

/**
 * @Author yanggld
 * @Date 2019/6/24-13:52
 * @Description
 */
@Configuration
public class DruidConfig {

	//配置Druid监控
	//1.配置管理后台的Servlet
	@Bean
	public ServletRegistrationBean<StatViewServlet> statViewServlet(){
		ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
		//com.alibaba.druid.support.http.ResourceServlet
		Map<String,String> initParams = Maps.newHashMap();
		initParams.put("loginUsername","admin");//登录后台时的用户名
		initParams.put("loginPassword","123456");//登录后台时的密码
		initParams.put("allow","");//默认就是允许所有的访问
		initParams.put("deny","");//拒绝
		servletRegistrationBean.setInitParameters(initParams);
		return servletRegistrationBean;
	}

	//2.配置一个web监控的过滤器
	@Bean
	public FilterRegistrationBean webStatFilter(){
		FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
		filterRegistrationBean.setFilter(new WebStatFilter());
		Map<String,String> initParams = Maps.newHashMap();
		initParams.put("exclusions","*.js,*.css,/druid/*");//不拦截的请求
		filterRegistrationBean.setInitParameters(initParams);
		filterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));
		return filterRegistrationBean;
	}
}
